﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Smartdispatch
{
    public partial class TreeShow : Form
    {
        static string cntstr = $@"Data Source=192.168.0.86;Initial Catalog=Tabwater_dev_new;User ID=sa;Password=abc123!@#";
        private static SqlConnection sqlCnt = new SqlConnection(cntstr);
        public TreeShow()
        {
            InitializeComponent();
            sqlCnt.Open();
            this.Disposed += TreeShow_Disposed;        }

        private void TreeShow_Disposed(object sender, EventArgs e)
        {
            sqlCnt.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            FlowChart flowChart = new FlowChart();
            int? flor;
            string cmdsql;
            //textBox1.Text = flowChart.ID.ToString();
            flowChart.Floor = int.Parse(textBox2.Text);
            flowChart.Run = checkBox2.Checked;
            flowChart.Result = checkBox3.Checked;
            flowChart.Name = textBox3.Text;
            flowChart.Remarks = textBox4.Text;
            if (comboBox1.Text.Length < 2)
            {
                flowChart.ParentID = null;
                flowChart.ParentLeft = null;
            }
            else
            {
                flowChart.ParentID = Guid.Parse(comboBox1.Text);
                flowChart.ParentLeft = checkBox1.Checked;
            }
            if (flowChart.ParentID == null)
            {
                flor = null;
                 cmdsql = $@"insert into K_FlowChart(ID,Name,Remarks,Floor,Run,Result,Mode)Values('{flowChart.ID}','" + flowChart.Name + "','" + flowChart.Remarks + "'," + flowChart.Floor.ToString() + "," + (flowChart.Run ? 1 : 0) + "," + (flowChart.Result ? 1 : 0).ToString() + "," + textBox5.Text + ")";
            }
            else
            {
                flor = (bool)flowChart.ParentLeft ? 1 : 0;
                 cmdsql = $@"insert into K_FlowChart(ID,ParentID,ParentLeft,Name,Remarks,Floor,Run,Result,Mode)Values('{flowChart.ID}','" + flowChart.ParentID.ToString() + "'," + flor.ToString() + ",'" + flowChart.Name + "','" + flowChart.Remarks + "'," + flowChart.Floor.ToString() + "," + (flowChart.Run ? 1 : 0) + "," + (flowChart.Result ? 1 : 0).ToString() + "," + textBox5.Text + ")";
            }
           



            label8.Text = cmdsql;

            comboBox1.Text = flowChart.ID.ToString();
            comboBox1.Items.Add(flowChart.ID.ToString());

            //SqlConnection sqlCnt = new SqlConnection(cntstr);
            
            SqlCommand command = new SqlCommand(cmdsql, sqlCnt);
            command.ExecuteNonQuery();
            //sqlCnt.Close();
          
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Treesdell(textBox6.Text.Trim(), int.Parse(textBox7.Text.Trim()));
        }
        private void Treesdell(string id,int rootnum)
        {
            List<string> vs = new List<string>();
            //string cntstr = $@"Data Source=192.168.0.86;Initial Catalog=Tabwater_dev_new;User ID=sa;Password=abc123!@#";
            string sqlstr = $@"update K_FlowChart set Floor={rootnum} where id='{id}'";
            
            SqlCommand command = new SqlCommand(sqlstr, sqlCnt);
            command.ExecuteNonQuery();
            sqlstr = $@"select id from K_FlowChart where Parentid='{id.ToLower()}'";
            command = new SqlCommand(sqlstr, sqlCnt);
            var result=command.ExecuteReader();
            while(result.Read())
            {
                vs.Add(result["id"].ToString().ToLower());
            }
            result.Close();
            
            foreach(var s in vs)
            {
                Treesdell(s, rootnum + 1);
            }
        }
    }
}
